/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.classes;

import br.com.fatecpg.mdi.Conexão;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class UsuarioAcessaDados {
    
    private Conexão conexao = new Conexão();
    private Connection conn;
    private PreparedStatement pstmt;
    
    public boolean incluir(ModeloUsuario u) throws SQLException {
        String insertSql = "INSERT INTO usuario(nm_usuario, cd_cpf_usuario, ic_ativo_sim_nao, ic_adm_user, cd_senha_usuario) " +
                "VALUES(?, ?, ?, ?, ?)";
        try {
            conn = conexao.getConexao();
            //conn = conexao.getConectar;
            pstmt = conn.prepareStatement(insertSql);
            pstmt.setString(1, u.getNome());
            pstmt.setString(2, u.getCpf());
            pstmt.setInt(3, u.getAtivo());
            pstmt.setInt(4, u.getUsuarioAdmin());
            pstmt.setString(5, u.getSenha());
            
            
            return pstmt.execute();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            conexao.fecharConexao();
        }
    }
    
    public int alterar(ModeloUsuario u) throws SQLException {
        String updateSql = "UPDATE usuario SET nm_usuario = ?, cd_cpf_usuario = ?, ic_ativo_sim_nao = ?, ic_adm_user = ?, cd_senha_usuario = ? " +
               "WHERE id_usuario = ?";
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(updateSql);
            pstmt.setString(1, u.getNome());
            pstmt.setString(2, u.getCpf());
            pstmt.setInt(3, u.getAtivo());
            pstmt.setInt(4, u.getUsuarioAdmin());
            pstmt.setString(5, u.getSenha());
            pstmt.setInt(6, u.getId());
            
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            conexao.fecharConexao();
        }
    }
    
    public ArrayList<ModeloUsuario> consulta(String filtro) throws SQLException{
        ArrayList<ModeloUsuario> lista = new ArrayList<ModeloUsuario>();
        String selectSql = "SELECT * FROM usuario WHERE " + filtro;
        ResultSet retornoConsulta = null;
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(selectSql);
            retornoConsulta = pstmt.executeQuery();
            
            while (retornoConsulta.next()) {
                ModeloUsuario u = new ModeloUsuario();
                u.setId(retornoConsulta.getInt(1));
                u.setNome(retornoConsulta.getString(2));
                u.setCpf(retornoConsulta.getString(3));
                u.setAtivo(retornoConsulta.getInt(4));
                u.setUsuarioAdmin(retornoConsulta.getInt(5));
                u.setSenha(retornoConsulta.getString(6));
                
                lista.add(u);
            }
            
            return lista;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (retornoConsulta != null) {
                retornoConsulta.close();
            }
            conexao.fecharConexao();
           
        }
    }
    
    public int consultaMaxId() throws SQLException {
        String selectSql = "SELECT MAX(id_usuario) FROM usuario";
        ResultSet retornoConsulta = null;
        int maxID = 0;
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(selectSql);
            retornoConsulta = pstmt.executeQuery();
            
            if (retornoConsulta.next()) {
                maxID = retornoConsulta.getInt(1);
            }
            
            return maxID;
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (retornoConsulta != null) {
                retornoConsulta.close();
            }
            conexao.fecharConexao();
           
        }
    }
    
    public int deletar(int id) throws SQLException {
        String deleteSql = "DELETE FROM usuario WHERE id_usuario = ?";
        
        try {
            conn = conexao.getConexao();
            pstmt = conn.prepareStatement(deleteSql);
            pstmt.setInt(1, id);
            
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            conexao.fecharConexao();
           
        }
    }
}
